package com.sdo.qihang.gprogress.lib;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ProgressBar;


public class GProgressHelper {

    private Context mContext;

    private ProgressBar mView;

    private static final int DEFAULT_TEXT_SIZE = 10;

    private static final int DEFAULT_TEXT_COLOR = 0X00000000;

    private static final int DEFAULT_COLOR_UNREACHED_COLOR = 0x00000000;

    private static final int DEFAULT_HEIGHT_REACHED_PROGRESS_BAR = 2;

    private static final int DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR = 2;

    private static final int DEFAULT_SIZE_TEXT_OFFSET = 10;

    protected boolean mSlideEnable = true;

    //进度数字颜色
    protected int mTextColor = DEFAULT_TEXT_COLOR;

    //进度数字字体大小
    protected int mTextSize = 0;

    //进度条偏移量
    protected int mTextOffset = 0;

    //进度条高度
    protected int mReachedProgressBarHeight = 0;

    //进度条颜色
    protected int mReachedBarColor = DEFAULT_TEXT_COLOR;

    //进度条背景颜色
    protected int mUnReachedBarColor = DEFAULT_COLOR_UNREACHED_COLOR;

    //进度条背景高度
    protected int mUnReachedProgressBarHeight = 0;

    //显示的进度条真实长度
    protected int mRealWidth;

    //是否显示进度文字
    protected boolean mIfDrawText = true;

    //是否可见进度文字
    protected static final int VISIBLE = 0;

    //背景边框线的宽度
    protected int mSlideWidth = 0;

    //背景边框与进度文字的边距
    protected int mSlidePadding = 0;

    //滑块边框样式
    protected Paint.Style mSlideLineStyle = Paint.Style.STROKE;

    //滑块文字画笔
    protected Paint mSlideTextPaint = new Paint();

    //滑块边框
    protected Paint mSlideLinePaint = new Paint();

    //背景边框画笔
    protected Paint mSlidePaint = new Paint();

    //初始化画笔
    protected Paint mPaint = new Paint();

    //背景颜色
    protected int mSlideBorderColor;

    //滑块边框线颜色
    protected int mSlideLineColor = DEFAULT_COLOR_UNREACHED_COLOR;

    protected int mProgressOverMax = 0;

    protected RectF rectF = new RectF(0, 0, 0, 0);

    public GProgressHelper(ProgressBar view, Context context, AttributeSet attrs, int defStyle) {
        mView = view;
        mContext = context;
        init(attrs, context);
    }

    public void drawProgress(Canvas canvas) {
        canvas.save();
        canvas.translate(mView.getPaddingLeft(), mView.getHeight() / 2);
        boolean noNeedBg = false;
        float radio = mView.getProgress() * 1.0f / mView.getMax();
        float progressPosX = (int) (mRealWidth * radio);
        String text;
        if (mSlideEnable) {
            text = mView.getProgress() + "%";
        } else {
            if (mProgressOverMax <= 100) {
                text = mView.getProgress() + "%";
            } else {
                text = mProgressOverMax + "%";
            }
        }

        float textWidth = mPaint.measureText(text);
        float textHeight = (mPaint.descent() + mPaint.ascent()) / 2;
        if (progressPosX + textWidth > mRealWidth && mIfDrawText) {
            progressPosX = mRealWidth - textWidth;
            noNeedBg = true;
        }
        float endX;
        if (mIfDrawText) {
            endX = progressPosX - mTextOffset / 2;
        } else {
            endX = progressPosX;
        }
        if (!noNeedBg) {
            float start;
            if (mIfDrawText) {
                start = progressPosX + mTextOffset / 2 + textWidth;
            } else {
                start = progressPosX;
            }
            mPaint.setColor(mUnReachedBarColor);
            mPaint.setStrokeWidth(mUnReachedProgressBarHeight);
            canvas.drawLine(start, 0, mRealWidth, 0, mPaint);
        }
        if (endX > 0) {
            mPaint.setColor(mReachedBarColor);
            mPaint.setStrokeWidth(mReachedProgressBarHeight);
            canvas.drawLine(0, 0, endX, 0, mPaint);
        }
        if (mIfDrawText) {
            //画滑块
            rectF.left = progressPosX - mSlidePadding - mSlideWidth;
            rectF.top = textHeight - mSlidePadding - mSlideWidth;
            rectF.right = progressPosX + textWidth + mSlidePadding + mSlideWidth;
            rectF.bottom = -textHeight + mSlidePadding + mSlideWidth;
            canvas.drawRoundRect(rectF, Utils.sp2px(13, mContext), Utils.sp2px(13, mContext), mSlidePaint);
            canvas.drawRoundRect(rectF, Utils.sp2px(13, mContext), Utils.sp2px(13, mContext), mSlideLinePaint);
            mPaint.setColor(mTextColor);
            canvas.drawText(text, progressPosX, -textHeight, mSlideTextPaint);
        }
        canvas.restore();
    }

    public void measure() {
        mRealWidth = mView.getMeasuredWidth() - mView.getPaddingRight() - mView.getPaddingLeft() - mSlideWidth * 2 - mSlidePadding * 2;
    }

    public int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = View.MeasureSpec.getMode(measureSpec);
        int specSize = View.MeasureSpec.getSize(measureSpec);
        if (specMode == View.MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            float textHeight = (mPaint.descent() - mPaint.ascent());
            result = (int) (mView.getPaddingTop() + mView.getPaddingBottom() + Math.max(
                    Math.max(mReachedProgressBarHeight,
                            mUnReachedProgressBarHeight), Math.abs(textHeight)));
            if (specMode == View.MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    private void init(AttributeSet attrs, Context context) {
        initDefault(context);
        obtainStyledAttributes(attrs);
        //初始化画笔
        initProgressPaint();
        initSlideTextPaint();
        initSlidePaint();
        initSlideLinePaint();
        mView.setPadding(Utils.dp2Px(mSlidePadding/2, mContext), Utils.dp2Px(mSlidePadding, mContext), Utils.dp2Px(mSlidePadding/2, mContext), Utils.dp2Px(mSlidePadding, mContext));
    }

    private void initDefault(Context context) {
        mTextSize = Utils.sp2px(DEFAULT_TEXT_SIZE, context);
        mTextOffset = Utils.dp2Px(DEFAULT_SIZE_TEXT_OFFSET, context);
        mReachedProgressBarHeight = Utils.dp2Px(DEFAULT_HEIGHT_REACHED_PROGRESS_BAR, context);
        mUnReachedProgressBarHeight = Utils.dp2Px(DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR, context);
    }

    private void obtainStyledAttributes(AttributeSet attrs) {
        // 初始化自定义属性
        final TypedArray attributes = mContext.obtainStyledAttributes(
                attrs, R.styleable.GProgress);

        mTextColor = attributes.getColor(R.styleable.GProgress_progressTextColor, DEFAULT_TEXT_COLOR);
        mTextSize = (int) attributes.getDimension(R.styleable.GProgress_progressTextSize, mTextSize);
        mReachedBarColor = attributes.getColor(R.styleable.GProgress_progressReachedColor, mTextColor);
        mUnReachedBarColor = attributes.getColor(R.styleable.GProgress_progressUnreachedColor, DEFAULT_COLOR_UNREACHED_COLOR);
        mReachedProgressBarHeight = (int) attributes.getDimension(R.styleable.GProgress_progressReached_bar_height, mReachedProgressBarHeight);
        mUnReachedProgressBarHeight = (int) attributes.getDimension(R.styleable.GProgress_progressUnreached_bar_height, mUnReachedProgressBarHeight);
        mTextOffset = (int) attributes.getDimension(R.styleable.GProgress_progressTextOffset, mTextOffset);
        mSlideEnable = attributes.getBoolean(R.styleable.GProgress_progressSlideEnable, true);
        mSlideWidth = (int) attributes.getDimension(R.styleable.GProgress_progressSlideWidth, 0f);
        mSlidePadding = (int) attributes.getDimension(R.styleable.GProgress_progressSlidePadding, 0f);
        mSlideBorderColor = attributes.getColor(R.styleable.GProgress_progressSlideColor, ContextCompat.getColor(mContext, R.color.c_00000000));
        mSlideLineColor = attributes.getColor(R.styleable.GProgress_progressSlideLineColor, ContextCompat.getColor(mContext, R.color.c_00000000));
        int textVisible = attributes.getInt(R.styleable.GProgress_progressTextVisibility, VISIBLE);
        if (textVisible != VISIBLE) {
            mIfDrawText = false;
        }
        attributes.recycle();
    }

    private void initProgressPaint() {
        //初始化画笔
        mPaint = new Paint();
        mPaint.setTextSize(mTextSize);
        mPaint.setColor(mTextColor);
        mPaint.setStrokeWidth(16);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setAntiAlias(true);
    }

    private void initSlidePaint() {
        //背景边框画笔
        mSlidePaint = new Paint();
        mSlidePaint.setStrokeWidth(mSlideWidth);
        mSlidePaint.setColor(mSlideBorderColor);
        mSlidePaint.setAntiAlias(true);
    }

    private void initSlideTextPaint() {
        //滑块文字画笔
        mSlideTextPaint = new Paint();
        mSlideTextPaint.setColor(mTextColor);
        mSlideTextPaint.setTextSize(mTextSize);
        mSlideTextPaint.setAntiAlias(true);
    }

    private void initSlideLinePaint() {
        //滑块边框画笔
        mSlideLinePaint = new Paint();
        mSlideLinePaint.setStyle(mSlideLineStyle);
        mSlideLinePaint.setStrokeWidth(mSlideWidth);
        mSlideLinePaint.setColor(mSlideLineColor);
        mSlideLinePaint.setAntiAlias(true);
    }

    public void setSlideEnable(boolean mSlideEnable) {
        this.mSlideEnable = mSlideEnable;
    }

    public boolean isSlideEnable() {
        return mSlideEnable;
    }


    public int getTextColor() {
        return mTextColor;
    }

    public void setTextColor(int mTextColor) {
        this.mTextColor = mTextColor;
    }

    public int getTextSize() {
        return mTextSize;
    }

    public void setTextSize(int mTextSize) {
        this.mTextSize = mTextSize;
    }

    public int getReachedProgressBarHeight() {
        return mReachedProgressBarHeight;
    }

    public void setReachedProgressBarHeight(int mReachedProgressBarHeight) {
        this.mReachedProgressBarHeight = mReachedProgressBarHeight;
    }

    public int getReachedBarColor() {
        return mReachedBarColor;
    }

    public void setReachedBarColor(int mReachedBarColor) {
        this.mReachedBarColor = mReachedBarColor;
    }

    public int getUnReachedBarColor() {
        return mUnReachedBarColor;
    }

    public void setUnReachedBarColor(int mUnReachedBarColor) {
        this.mUnReachedBarColor = mUnReachedBarColor;
    }

    public int getUnReachedProgressBarHeight() {
        return mUnReachedProgressBarHeight;
    }

    public void setUnReachedProgressBarHeight(int mUnReachedProgressBarHeight) {
        this.mUnReachedProgressBarHeight = mUnReachedProgressBarHeight;
    }

    public boolean isIfDrawText() {
        return mIfDrawText;
    }

    public void setIfDrawText(boolean mIfDrawText) {
        this.mIfDrawText = mIfDrawText;
    }

    public int getSlidePadding() {
        return mSlidePadding;
    }

    public void setSlidePadding(int mSlidePadding) {
        this.mSlidePadding = mSlidePadding;
    }

    public int getSlideBorderColor() {
        return mSlideBorderColor;
    }

    public void setSlideBorderColor(int mSlideBorderColor) {
        this.mSlideBorderColor = mSlideBorderColor;
    }

    public int getSlideLineColor() {
        return mSlideLineColor;
    }

    public void setSlideLineColor(int mSlideLineColor) {
        this.mSlideLineColor = mSlideLineColor;
    }

    public int getProgressOverMax() {
        return mProgressOverMax;
    }

    public void setProgressOverMax(int progress) {
        mView.setProgress(progress);
        this.mProgressOverMax = progress;
    }
}
